package com.ljz.service.impl;

import com.ljz.api.SpuClint;
import com.ljz.api.bean.SpuBean;
import com.ljz.common.core.exception.BusinessException;
import com.ljz.common.dao.MyBaseServiceImpl;
import com.ljz.common.utils.Assert;
import com.ljz.common.utils.DateUtil;
import com.ljz.common.utils.ReturnResult;
import com.ljz.entity.Order;
import com.ljz.entity.OrderAddr;
import com.ljz.entity.OrderItem;
import com.ljz.mapper.OrderAddrMapper;
import com.ljz.mapper.OrderItemMapper;
import com.ljz.mapper.OrderMapper;
import com.ljz.service.OrderAddrService;
import com.ljz.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl extends MyBaseServiceImpl<Order, OrderMapper> implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderAddrMapper orderAddrMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private SpuClint spuClint;

    private final String PREFIX = "YHDD";
    private String maxCode = null;

    /**
     * 添加订单
     * @param entity
     * @return
     */
    @GlobalTransactional
    @Transactional
    @Override
    public int saveEntity(Order entity) {
        OrderAddr orderAddr = entity.getOrderAddr();
        //添加订单地址
        int addrRe= orderAddrMapper.insert(orderAddr);
        Assert.isSuccess(addrRe,"添加订单地址失败");

        Long orderAddr_id=orderAddr.getOrder_addr_id();
        entity.setOrder_addr_id(orderAddr_id);
        //添加订单
        entity.setOrder_code(generateCode());
        int res=baseMapper.insert(entity);
        Assert.isSuccess(res,"添加订单失败");
        //添加订单明细
        List<OrderItem> orderItems = entity.getOrderItems();
        if(orderItems!=null&&orderItems.size()>0){
            for (OrderItem orderItem : orderItems) {
                orderItem.setOrder_id(orderAddr_id);
            }
            orderItemMapper.insert(orderItems);
            //修改库存
            List<SpuBean> list=new ArrayList<>();
            for (OrderItem orderItem : orderItems) {
                SpuBean spuBean = new SpuBean();
                spuBean.setSpu_id(orderItem.getSpu_id());
                spuBean.setCount(orderItem.getCount());
                spuBean.setOrder_id(entity.getOrder_id());
                spuBean.setSku_id(orderItem.getSku_id());
                list.add(spuBean);
            }
            ReturnResult returnResult=spuClint.updateStock(list);
            if (returnResult.getCode()!=200){
                throw new BusinessException(returnResult.getMessage());
            }
        }
        return res;
    }

    @Override
    public Order getOrderByCode(String code) {
        return orderMapper.getOrderByCode(code);
    }

    @Override
    public int deleteById(String orderCode) {
        int re=orderMapper.deleteById(orderCode);
        Assert.isSuccess(re,"根据订单编号删除订单失败");
        return re;
    }

    /**
     * 批量添加订单
     * @param orders
     * @return
     */
    @GlobalTransactional
    @Transactional
    @Override
    public int saveBatch(List<Order> orders) {
        for (Order order : orders) {
            OrderAddr orderAddr = order.getOrderAddr();
            //添加订单地址
            int addrRe= orderAddrMapper.insert(orderAddr);
            Assert.isSuccess(addrRe,"添加订单地址失败");

            Long orderAddr_id=orderAddr.getOrder_addr_id();
            order.setOrder_addr_id(orderAddr_id);
            //添加订单
            order.setOrder_code(generateCode());
            int res=baseMapper.insert(order);
            Assert.isSuccess(res,"添加订单失败");
            //添加订单明细
            List<OrderItem> orderItems = order.getOrderItems();
            if(orderItems!=null&&orderItems.size()>0){
                for (OrderItem orderItem : orderItems) {
                    orderItem.setOrder_id(orderAddr_id);
                }
                orderItemMapper.insert(orderItems);
                //修改库存
                List<SpuBean> list=new ArrayList<>();
                for (OrderItem orderItem : orderItems) {
                    SpuBean spuBean = new SpuBean();
                    spuBean.setSpu_id(orderItem.getSpu_id());
                    spuBean.setCount(orderItem.getCount());
                    spuBean.setOrder_id(order.getOrder_id());
                    spuBean.setSku_id(orderItem.getSku_id());
                    list.add(spuBean);
                }
                ReturnResult returnResult=spuClint.updateStock(list);
                if (returnResult.getCode()!=200){
                    throw new BusinessException(returnResult.getMessage());
                }
            }
        }
        return 1;
    }

    @Override
    public Long getOrdersActualAmount(List<String> orderCodes) {
        return orderMapper.getOrdersActualAmount(orderCodes);
    }


    /**
     * 生成编码
     * @return
     */
    String generateCode(){
        String date = DateUtil.getYm();
        if(null == maxCode){
            maxCode = baseMapper.maxCode(date);
            if(maxCode==null || "".equals(maxCode)){
                maxCode = PREFIX+date+"001";
                return maxCode;
            }
        }
        String month = maxCode.substring(4,10);
        if(!date.equals(month)){
            maxCode = PREFIX+date+"001";
            return maxCode;
        }
        String num = maxCode.substring(10);
        maxCode = PREFIX+date+String.format("%03d",(Integer.parseInt(num)+1));
        return maxCode;
    }
}
